home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / Amiga_Mail_Vol2 / Archives / Plain / so91.lha / Scan / listdir2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-23  |  6.6 KB  |  227 lines

  1. ;/* ListDir2.c   AmigaMail seconds ExAll() example.
  2. lc -cfis -v -d0 -b0 -j73 ListDir2.c
  3. blink from ListDir2.o to ListDir2 lib lib:amiga.lib ;if you don't have pragmas
  4. quit
  5.  *
  6.  * Pure code if pragmas are used.
  7.  * Tuesday, 16-Jul-91 16:21:14, Ewout
  8.  *
  9.  * Compiled with SAS/C 5.10a
  10.  */
  11.  
  12. /* (c)  Copyright 1991 Commodore-Amiga, Inc.   All rights reserved.
  13. The information contained herein is subject to change without notice,
  14. and is provided "as is" without warranty of any kind, either expressed
  15. or implied.  The entire risk as to the use of this information is
  16. assumed by the user.
  17. */
  18.  
  19. #include <exec/memory.h>
  20. #include <dos/dosextens.h>
  21. #include <dos/rdargs.h>
  22. #include <dos/exall.h>
  23. #include <utility/hooks.h>
  24.  
  25. #include <clib/exec_protos.h>
  26. #include <clib/dos_protos.h>
  27. #include <clib/utility_protos.h>
  28.  
  29. /* undef PRAGMAS if you don't have them */
  30. #define PRAGMAS
  31. #undef PRAGMAS
  32. #ifdef PRAGMAS
  33. #include <pragmas/exec_pragmas.h>
  34. #include <pragmas/dos_pragmas.h>
  35. #include <pragmas/utility_pragmas.h>
  36. #else
  37. struct ExecBase *SysBase;
  38. struct DosLibrary *DOSBase;
  39. struct Library *UtilityBase;
  40.  
  41. #endif
  42.  
  43. /* Buffersize to receive filenames in */
  44. #define BUFFERSIZE 512
  45.  
  46.  
  47. VOID            main(VOID);
  48. UWORD           StrLen(UBYTE *);
  49.  
  50. /* SAS/C specific, use asm stub otherwise */
  51. #define ASM __asm
  52. #define REG(x) register __## x
  53. BOOL ASM  ExAllHook(REG(a0) struct Hook * hook,
  54.                     REG(a1) struct ExAllData * data,
  55.                     REG(a2) LONG * datatype);
  56.  
  57. VOID
  58. main(VOID)
  59. {
  60. #ifdef PRAGMAS
  61.     struct DosLibrary *DOSBase;
  62.     struct Library *UtilityBase;
  63.  
  64. #endif
  65.     struct RDArgs  *readargs;
  66.     LONG            rargs[4];
  67.     struct ExAllControl *excontrol;
  68.     struct ExAllData *ead, *buffer;
  69.     struct Hook     exallhook;
  70.     UBYTE          *pattern, *parsebuffer;
  71.     BPTR            sourcelock;
  72.     BOOL            exmore;
  73.     COUNT           i;
  74.     LONG            parselength, type, error;
  75.  
  76. #ifndef PRAGMAS
  77.     /* set up SysBase */
  78.     SysBase = (*((struct Library **) 4));
  79. #endif
  80.  
  81.     /* Fail silently if < 37 */
  82.     if (DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37))
  83.     {
  84.         UtilityBase = DOSBase->dl_UtilityBase;
  85.  
  86.         rargs[1] = 0L;
  87.         rargs[2] = 0L;
  88.         if (readargs = ReadArgs("PATTERN/A,DIRS/S,FILES/S", rargs, NULL))
  89.         {
  90.  
  91.             pattern = (UBYTE *) rargs[0];
  92.  
  93.             /*
  94.              * If DIRS or files not specified or both, accept
  95.              * both files and directories
  96.              */
  97.             if (rargs[1] == rargs[2])
  98.                 type = 0;
  99.             else
  100.             {
  101.                 /* Accept only directories */
  102.                 if (rargs[1])
  103.                     type = 1;
  104.                 /* Accept only files */
  105.                 else
  106.                     type = -1;
  107.             }
  108.  
  109.             parselength = StrLen(pattern) * 3;
  110.             if (parsebuffer = AllocMem(parselength, MEMF_CLEAR))
  111.             {
  112.  
  113.                 /* Make pattern uppercase for possible character classes */
  114.                 i = 0;
  115.                 while (pattern[i])
  116.                     pattern[i] = ToUpper(pattern[i++]);
  117.  
  118.                 if ((ParsePatternNoCase(pattern, parsebuffer, parselength)) != -1)
  119.                 {
  120.  
  121.                     if (buffer = AllocMem(BUFFERSIZE, MEMF_CLEAR))
  122.                     {
  123.  
  124.                         sourcelock =
  125.                                 ((struct Process *) FindTask(NULL))->pr_CurrentDir;
  126.  
  127.                         if (excontrol = AllocDosObject(DOS_EXALLCONTROL, NULL))
  128.                         {
  129.  
  130.                             exallhook.h_Entry = ExAllHook;
  131.                             exallhook.h_Data = (VOID *) type;
  132.  
  133.                             excontrol->eac_MatchString = parsebuffer;
  134.                             excontrol->eac_MatchFunc = &exallhook;
  135.  
  136.                             do
  137.                             {
  138.  
  139.                                 exmore = ExAll(sourcelock,
  140.                                                buffer,
  141.                                                BUFFERSIZE,
  142.                                                ED_TYPE,
  143.                                                excontrol);
  144.                                 error = IoErr();
  145.                                 if ((exmore == NULL &&
  146.                                     (error != ERROR_NO_MORE_ENTRIES)))
  147.                                     break;
  148.  
  149.                                 if (excontrol->eac_Entries == 0)
  150.                                     continue;
  151.  
  152.                                 ead = buffer;
  153.                                 do
  154.                                 {
  155.  
  156.                                     /* Check for CTRL-C */
  157.                                     if (SetSignal(0L, SIGBREAKF_CTRL_C) &
  158.                                         SIGBREAKF_CTRL_C)
  159.                                     {
  160.                                         error = ERROR_BREAK;
  161.                                         exmore = FALSE;
  162.                                         break;
  163.                                     }
  164.  
  165.                                     rargs[0] = (LONG) ead->ed_Name;
  166.                                     VFPrintf(Output(), "%s\n", rargs);
  167.  
  168.                                     ead = ead->ed_Next;
  169.                                 } while (ead);
  170.                             } while (exmore);
  171.  
  172.                             if (error != ERROR_NO_MORE_ENTRIES)
  173.                                 PrintFault(error, NULL);
  174.  
  175.                             FreeDosObject(DOS_EXALLCONTROL, excontrol);
  176.                         }
  177.                         else
  178.                             PrintFault(ERROR_NO_FREE_STORE, NULL);
  179.  
  180.                         FreeMem(buffer, BUFFERSIZE);
  181.                     }
  182.                     else
  183.                         PrintFault(ERROR_NO_FREE_STORE, NULL);
  184.                 }
  185.                 else
  186.                     PrintFault(ERROR_BAD_TEMPLATE, NULL);
  187.                 FreeMem(parsebuffer, parselength);
  188.             }
  189.             else
  190.                 PrintFault(ERROR_NO_FREE_STORE, NULL);
  191.             FreeArgs(readargs);
  192.  
  193.         }
  194.         else
  195.             PrintFault(IoErr(), NULL);
  196.         CloseLibrary((struct Library *) DOSBase);
  197.     }
  198. }
  199.  
  200. BOOL ASM
  201. ExAllHook(REG(a0) struct Hook * hook,
  202.           REG(a1) struct ExAllData * data,
  203.           REG(a2) LONG * datatype)
  204. {
  205.     LONG            neededfiletype = (LONG) hook->h_Data;
  206.     BOOL            success = TRUE;
  207.  
  208.     if (neededfiletype != 0)
  209.     {
  210.         if (data->ed_Type > 0 && neededfiletype < 0)
  211.             success = FALSE;
  212.         if (data->ed_Type < 0 && neededfiletype > 0)
  213.             success = FALSE;
  214.     }
  215.     return (success);
  216. }
  217.  
  218.  
  219. UWORD
  220. StrLen(UBYTE * string)
  221. {
  222.     UBYTE          *length = string + 1;
  223.  
  224.     while (*string++ != '\0');
  225.     return ((UWORD) (string - length));
  226. }
  227.